package com.aididi;

import com.aididi.service.FeignBackService;
import com.aididi.service.RibbonBackService;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableHystrix
@EnableDiscoveryClient
@EnableFeignClients({"com.aididi.service"})
@RestController
//开启断路器
// @EnableCircuitBreaker		//@EnableHystrix注解包括@EnableCircuitBreaker注解
public class ServiceHystrixApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceHystrixApplication.class, args);
	}

	@Bean
	@LoadBalanced
	RestTemplate restTemplate(){
		return new RestTemplate();
	}

	@Autowired
	RibbonBackService ribbonBackService;

	@Autowired
	FeignBackService feignBackService;

	/**
	 * 使用ribbon+restTemplate是调用hystrix熔断
	 * @param name
	 * @return
	 */
	@GetMapping("/ribbonback")
	public String hiRibbonFallBack(@RequestParam String name){
		return ribbonBackService.sayHi(name);
	}

	@GetMapping("/feignback")
	public String hiFeignFallBack(@RequestParam String name){
		return feignBackService.getObjFromClient(name);
	}







}
